package ee.videorentalstore;

import ee.videorentalstore.protocols.Protocol;
import ee.videorentalstore.protocols.types.FilmManagerProtocol;
import ee.videorentalstore.users.User;
import ee.videorentalstore.users.types.FilmInventoryManager;
import ee.videorentalstore.utils.UserInputReader;


public class UserAndProtocolManager 
{
	private static UserAndProtocolManager instance = null;
	private User currentUser = new FilmInventoryManager();
	private Protocol protocolInUse = new FilmManagerProtocol();
	private UserAndProtocolManagerStatus userAndProtocolManagerStatus;
	private boolean programIsNotRequestedToShutDown = true;
	
	
	private UserAndProtocolManager()
	{
		userAndProtocolManagerStatus = UserAndProtocolManagerStatus.FOLLOWING_CURRENT_PROTOCOL;
	}
	

	public static UserAndProtocolManager getInstance()
	{
		if (instance == null)
		{
			instance = new UserAndProtocolManager();
		}
		return instance;
	}
	
	
	public void openVideoRentalStore()
	{
		//sysouti kirjutatakse igast infot
		System.out.println("WELCOME TO OUR VIDEO RENTAL STORE SERVICE!\n\n" +
				"Current user is film manager!\n\n" +
				"Using our service is easy. You just have to give me commands in format\n" +
				"command----command contents\n" +
				"You'll get a list of all possible commands and command contents structure expected by typing HELP at any time.\n" +
				"Note that you'll be seeing only commands which you are authorized to use.\n" +
				"Therefore if you want to exit the system for example, you have to be an employee of a \n" +
				"video rental store, which in our case, is film manager.\n\n" +
				"To change user (to be a customer for example), type CHANGE USER at any time and act according to following instructions given by system!\n\n\n" +
				"Type command and press enter...");
		
		
		while (programIsNotRequestedToShutDown)
		{	
			switch (userAndProtocolManagerStatus) 
			{
				case FOLLOWING_CURRENT_PROTOCOL:
					while (userAndProtocolManagerStatus == UserAndProtocolManagerStatus.FOLLOWING_CURRENT_PROTOCOL)
					{
						runProtocolInUse();
					}
					break;
				
				case CHANGING_USER_AND_PROTOCOL:
					while (userAndProtocolManagerStatus == UserAndProtocolManagerStatus.CHANGING_USER_AND_PROTOCOL)
					{
						runProtocolInUse();
					}
					
					break;
					
				case SHUTTING_DOWN:
					programIsNotRequestedToShutDown = false;
					break;
				
				default:
					programIsNotRequestedToShutDown = false;
					System.out.println("Error controlling protocol flow!");
					break;
			}
		}
		
		UserInputReader.getInstance().closeCmdInputStream();
		System.out.println("Video rental service is shutting down!");
	}
	
	
	private void runProtocolInUse()
	{
		getProtocolInUse().runUserProtocol();
	}
	
	
	public User getCurrentUserInControl() 
	{
		return currentUser;
	}

	public void setUserWhoIsInControl(User userInControl)
	{
		this.currentUser = userInControl;
	}

	public Protocol getProtocolInUse() 
	{
		return protocolInUse;
	}

	public void setProtocolToUse(Protocol protocolToUse) 
	{
		this.protocolInUse = protocolToUse;
	}


	public UserAndProtocolManagerStatus getUserAndProtocolManagerStatus() 
	{
		return userAndProtocolManagerStatus;
	}


	public void setUserAndProtocolManagerStatus(
			UserAndProtocolManagerStatus userAndProtocolManagerStatus) 
	{
		this.userAndProtocolManagerStatus = userAndProtocolManagerStatus;
	}
}
